package com.sky.controller.admin;

import com.sky.entity.User;
import com.sky.result.Result;
import com.sky.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/user")
@Api(tags = "用户管理")
@Slf4j
public class UserController2 {

    @Autowired
    private UserService userService;

    @PostMapping
    @ApiOperation("新增用户")
    public Result save(@RequestBody User user) {
        userService.save(user);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除用户")
    public Result delete(@PathVariable Long id) {
        userService.deleteById(id);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改用户")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/{id}")
    @ApiOperation("根据ID查询用户")
    public Result<User> getById(@PathVariable Long id) {
        User user = userService.getById(id);
        return Result.success(user);
    }

    @GetMapping("/list")
    @ApiOperation("查询所有用户")
    public Result<List<User>> listAll() {
        List<User> users = userService.listAll();
        return Result.success(users);
    }
}